<template>
  <el-dialog :visible.sync="isShow" v-if="isShow" title="openInstall配置" width="55%" :before-close="handleClose" top="150px" :close-on-click-modal="false">
    <div class="border-box">
      <el-form ref="emailForm" :model="formParams" :rules="ruleForm" label-position="right" label-width="0">
        <div class="flex item-box" v-for="(item,index) in itemData" :key="index">
          <div class="title width25">
            {{ item.label }}<span :class="item.pass?'mustPass':'noMustPass'">*</span>
          </div>
          <div class="content width80">
            <el-form-item class="item-clear" v-show="item.key=='agent_id'" label="" prop="agent_id">
              <el-select v-model="formParams.agent_id" placeholder="请选择渠道" filterable >
                <el-option :label="'【'+item.value+'】'+item.label" :value="item.value" v-for="(item, index) in channelData" :key="index"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item class="item-clear" v-show="item.key=='channel_id'" label="" prop="channel_id">
              <el-input-number :min="0" :precision="0" v-model="formParams.channel_id" controls-position="right" clearable placeholder="请输入渠道channel_id" />
            </el-form-item>
            <el-form-item class="item-clear" v-show="item.key=='types'" label="" prop="types">
              <el-radio-group v-model="formParams.types">
                <el-radio-button :label="1">安卓</el-radio-button>
                <el-radio-button :label="2">IOS</el-radio-button>
              </el-radio-group>
            </el-form-item>
            <el-form-item class="item-clear" v-show="item.key=='is_close'" label="" prop="is_close">
              <el-radio-group v-model="formParams.is_close">
                <el-radio-button :label="1">关闭</el-radio-button>
                <el-radio-button :label="0">开启</el-radio-button>
              </el-radio-group>
            </el-form-item>
            <el-form-item class="item-clear" v-show="item.key=='is_upper_package'" label="" prop="is_upper_package">
              <el-radio-group v-model="formParams.is_upper_package">
                <el-radio-button :label="1">上架包</el-radio-button>
                <el-radio-button :label="0">普通包</el-radio-button>
              </el-radio-group>
            </el-form-item>
            <el-form-item class="item-clear" v-show="item.key=='app_key'" label="" prop="app_key">
              <el-input type="text" v-model="formParams.app_key" clearable placeholder="请输入AppKey"></el-input>
            </el-form-item>
          </div>
        </div>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button size="medium" @click="handleClose">取 消</el-button>
      <el-button size="medium" type="primary" @click="handleConfirm('emailForm')">确 认</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  addOpenInstall,
  editOpenInstall
} from "@/api/config";

export default {
  components: {},
  props: {
    show: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object,
      default: function () {
        return {}
      }
    },
    channelData: {
      type: Array,
      default: function () {
        return []
      }
    },
  },
  data () {
    return {
      isShow: false,
      itemData:[
        { label: '代理agent_id', key: 'agent_id' ,},
        { label: '渠道channel_id', key: 'channel_id' ,pass:true},
        { label: '类型', key: 'types' },
        { label: '是否开启', key: 'is_close' },
        { label: '是否是上架包', key: 'is_upper_package' },
        { label: 'AppKey', key: 'app_key' },
      ],
      formParams: {
        agent_id: '',
        types: 1,
        is_close: 0,
        is_upper_package: 1,
        app_key: '',
        channel_id: 0,
      },
      ruleForm: {
        channel_id: [{ required: true, trigger: 'blur', message: '请输入渠道channel_id', }],
      },
    }
  },
  computed: {},
  watch: {
    show: function () {
      let that = this;
      that.isShow = that.show
      if (that.data) {
        that.formParams = JSON.parse(JSON.stringify(that.data))
      } else {
        that.formParams = {
          agent_id: '',
          types: 1,
          is_close: 0,
          is_upper_package: 1,
          channel_id: 0,
          app_key: '',
        }
      }
    },
  },
  mounted () { },
  methods: {
    // 确认
    handleConfirm (formvalue) {
      let that = this,
      params = JSON.parse(JSON.stringify(that.formParams));
      if(!params.channel_id){
        return that.$message.error('请输入渠道channel_id')
      }
      that.$refs[formvalue].validate((valid) => {
        if (valid) {
          if (!params.id) {
            addOpenInstall(params).then(function (res) {
              that.$message.success(res.msg)
              that.$emit('onHideDetails', true)
            }).catch((error) => {
              that.$message.error(error.msg)
            })
          } else {
            editOpenInstall(params).then(function (res) {
              that.$message.success(res.msg)
              that.$emit('onHideDetails', true)
            }).catch((error) => {
              that.$message.error(error.msg)
            })
          }
        } else {
          return false;
        }
      });
    },
    // 取消
    handleClose () {
      this.$emit('onHideDetails', false)
    },
  }
}
</script>

<style scoped lang="scss">
</style>
